home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / lib / Bitset.c < prev    next >
C/C++ Source or Header  |  1990-05-19  |  3KB  |  113 lines

  1. /* Bitset.c -- implementation of set of small integers
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet: kgorlen@alw.nih.gov
  20.     September, 1985
  21.  
  22. Function:
  23.     
  24. A Bitset is a set of small integers.  It is implemented very efficiently
  25. using a single word.  Each bit of the word indicates if the integer
  26. associated with the bit position is in the set.  Bitsets are
  27. particularly useful in conjunction with enum constants.
  28.  
  29. $Log:    Bitset.c,v $
  30.  * Revision 3.0  90/05/20  00:19:12  kgorlen
  31.  * Release for 1st edition.
  32.  * 
  33. */
  34.  
  35. #include "Bitset.h"
  36. #include "nihclIO.h"
  37.  
  38. #define    THIS    Bitset
  39. #define    BASE    Object
  40. #define BASE_CLASSES BASE::desc()
  41. #define MEMBER_CLASSES
  42. #define VIRTUAL_BASE_CLASSES Object::desc()
  43.  
  44. DEFINE_CLASS(Bitset,1,"$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/lib/RCS/Bitset.c,v 3.0 90/05/20 00:19:12 kgorlen Rel $",NULL,NULL);
  45.  
  46. unsigned Bitset::capacity() const    { return sizeof(int)*8; }
  47.  
  48. void Bitset::deepenShallowCopy()    {}
  49.  
  50. unsigned Bitset::hash() const    { return m; }
  51.     
  52. bool Bitset::isEmpty() const    { return m==0; }
  53.     
  54. bool Bitset::isEqual(const Object& ob) const
  55. {
  56.     return ob.isSpecies(classDesc) && *this==castdown(ob);
  57. }
  58.  
  59. const Class* Bitset::species() const { return &classDesc; }
  60.  
  61. void Bitset::printOn(ostream& s) const
  62. {
  63.     Bitset t = *this;
  64.     for (register unsigned i =0; i<capacity() && !t.isEmpty(); i++) {
  65.         if (t.includes(i)) {
  66.             s << i;
  67.             t -= i;
  68.             if (!t.isEmpty()) s << ',';
  69.         }
  70.     }
  71. }
  72.  
  73. unsigned Bitset::size() const
  74. {
  75.     register unsigned l=m;
  76.     register unsigned n=0;
  77.     while (l != 0) {
  78.         l &= (l-1);    // removes rightmost 1 
  79.         n++;
  80.     }
  81.     return n;
  82. }
  83.  
  84. Bitset::Bitset(OIOin& strm)
  85.     : BASE(strm)
  86. {
  87.     strm >> m;
  88. }
  89.  
  90. void Bitset::storer(OIOout& strm) const
  91. {
  92.     BASE::storer(strm);
  93.     strm << m;
  94. }
  95.  
  96. Bitset::Bitset(OIOifd& fd)
  97.     : BASE(fd)
  98. {
  99.     fd >> m;
  100. }
  101.  
  102. void Bitset::storer(OIOofd& fd) const
  103. {
  104.     BASE::storer(fd);
  105.     fd << m;
  106. }
  107.  
  108. int Bitset::compare(const Object&) const
  109. {
  110.     shouldNotImplement("compare");
  111.     return 0;
  112. }
  113.